<?php

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 * Description of Eorder
 *
 * @author maylor
 */
@session_start();

class Eorder extends WebBase {

    public final function donedo() {
        $para = $_REQUEST;
        $id = intval($para['id']);
        $sql = "select * from {$this->prename}member_recharge where rechargeId=? and isDelete=0";
        $data = $this->getRow($sql, $id);
        //判断一下这个订单是否完成或删除
        if (empty($data)) {
            $this->assign('msg', '订单不存在');
            $this->newdisplay('eorder/msg.php');
            exit;
        }
        if ($data['state'] == 3) {
            $this->assign('msg', '订单已驳回');
            $this->newdisplay('eorder/msg.php');
            exit;
        } elseif ($data['state'] == 0) {
            $this->assign('lists', $this->get_parms($data));
            $this->assign('data', $data);
            $this->newdisplay('eorder/done.php');
            exit;
        } else {
            $this->assign('msg', '订单已处理');
            $this->newdisplay('eorder/msg.php');
            exit;
        }
    }
    
    public final function successdo() {
        $this->assign('msg', '订单处理成功');
        $this->newdisplay('eorder/msg.php');
        exit;
    }

    //充值确认
    public final function rechargedo() {
        $para = $_POST;
        $id = intval($para['id']);
        $code = $para['password'];
        $lock_key = 'ziyuan_recharge_codelock_'.$id;
        $codelock = common_redis()->get($lock_key);
        $getamount = floatval($para['amount']);
        if($codelock && $codelock>=3) {
            $this->echo_json(['status'=>0,'msg'=>'安全码错误次数过多，本通道失效，请到后台确认订单。']);
        }
        $sql = "select * from {$this->prename}member_recharge where id=? and isDelete=0 and state=0";
        $data = $this->getRow($sql, $id);
        if(empty($data)) {
            $this->echo_json(['status'=>0,'msg'=>'订单不存在或不需要处理']);
        }
        if(empty($code) || $code!==$data['code']) {
            if(!$codelock) {
                common_redis()->set($lock_key,1);
            }else {
                common_redis()->incr($lock_key);
            }
            $this->echo_json(['status'=>0,'msg'=>'安全码错误']);
        }
        try {
            $this->beginTransaction();
            $amount = empty($getamount)?$data['amount']:$getamount;
            $para = array_merge(array('rechargeAmount' => $amount, 'state' => 1, 'info' => '手动确认', 'actionUid' => 1, 'rechargeTime' => $this->time, 'actionIP' => $this->ip(true)), $this->getRow("select coin, fcoin from {$this->prename}members where uid={$data['uid']}"));
            if ($this->updateRows($this->prename . 'member_recharge', $para, 'id=' . $data['id'])) {
                $this->addCoin(array(
                    'uid' => $data['uid'],
                    'coin' => $para['rechargeAmount'],
                    'liqType' => 1,
                    'extfield0' => $data['id'],
                    'extfield1' => $data['rechargeId'],
                    'info' => '充值'
                ));

                $this->update("UPDATE {$this->prename}order SET state = 2 WHERE state=0 and order_number = ?", $data['rechargeId']);
            }
            $this->addLog(2, '充值确认' . '[充值编号:' . $data['rechargeId'] . ']', $data['uid'], $data['username']);
            $this->commit();
            $this->echo_json(['status'=>1,'msg'=>'操作成功']);
        } catch (Exception $e) {
            $this->rollBack();
            $this->echo_json(['status'=>0,'msg'=>'操作失败']);
        }
    }
    
    //驳回
    public final function rechargenodo() {
        $para = $_POST;
        $id = intval($para['id']);
        $code = $para['password'];
        $lock_key = 'ziyuan_recharge_codelock_'.$id;
        $codelock = common_redis()->get($lock_key);
        if($codelock && $codelock>=3) {
            $this->echo_json(['status'=>0,'msg'=>'安全码错误次数过多，本通道失效，请到后台确认订单。']);
        }
        $sql = "select * from {$this->prename}member_recharge where id=? and isDelete=0 and state=0";
        $data = $this->getRow($sql, $id);
        if(empty($data)) {
            $this->echo_json(['status'=>0,'msg'=>'订单不存在或不需要处理']);
        }
        if(empty($code) || $code!==$data['code']) {
            if(!$codelock) {
                common_redis()->set($lock_key,1);
            }else {
                common_redis()->incr($lock_key);
            }
            $this->echo_json(['status'=>0,'msg'=>'安全码错误']);
        }
        
		if($this->updateRows($this->prename .'member_recharge', array('state'=>3), 'id='.$id)){
			$this->echo_json(['status'=>1,'msg'=>'操作成功']);
		}else{
			$this->echo_json(['status'=>0,'msg'=>'操作失败']);
		}
    }

    protected function get_parms($para) {
        $data_parms = [
            [
                'name' => '充值金额', 'values' => $para['amount']
            ],
            [
                'name' => '订单编号', 'values' => $para['rechargeId']
            ],
            [
                'name' => '用户名称', 'values' => $para['username']
            ],
            [
                'name' => '用户uid', 'values' => $para['uid']
            ],
            [
                'name' => '描述信息', 'values' => $para['depositinfo']
            ],
            [
                'name' => '类型', 'values' => $para['info']
            ],
            [
                'name' => '充值时间', 'values' => date('Y-m-d H:i:s', $para['actionTime'])
            ],
            [
                'name' => '操作IP', 'values' => long2ip($para['actionIP'])
            ],
            [
                'name' => '支付类型', 'values' => $para['rechType']
            ]
        ];
        return $data_parms;
    }
    
    
    public function echo_json($data) {
        header('Content-type:text/json'); 
        echo json_encode($data);
        exit;
    }
    
    /**
	 * 管理员日志
	 */
	public function addLog($type,$logString, $extfield0=0, $extfield1=''){
		$log=array(
			'uid'=>1,
			'username'=>'admin',
			'type'=>$type,
			'actionTime'=>$this->time,
			'actionIP'=>$this->ip(true),
			'action'=>$logString,
			'extfield0'=>$extfield0,
			'extfield1'=>$extfield1
		);
		return $this->insertRow($this->prename .'admin_log', $log);
	}
    
    /**
	 * 用户资金变动
	 *
	 * 请在一个事务里使用
	 */
	public function addCoin($log){

		if(!isset($log['uid'])) $log['info']=1;
		if(!isset($log['info'])) $log['info']='';
		if(!isset($log['coin'])) $log['coin']=0;
		if(!isset($log['type'])) $log['type']=0;
		if(!isset($log['fcoin'])) $log['fcoin']=0;
		if(!isset($log['extfield0'])) $log['extfield0']=0;
		if(!isset($log['extfield1'])) $log['extfield1']='';
		if(!isset($log['extfield2'])) $log['extfield2']='';
		$sql="call setCoin({$log['coin']}, {$log['fcoin']}, {$log['uid']}, {$log['liqType']}, {$log['type']}, '{$log['info']}', {$log['extfield0']}, '{$log['extfield1']}', '{$log['extfield2']}')";
		$this->insert($sql);

	}
    
    

}
